package com.itcoon.cloud.framework.springdoc.plugin;/**
 * @author Athric Yu
 * 2024/11/20
 **/

import com.itcoon.cloud.framework.properties.TemporalPatternConfigProperties;
import com.itcoon.cloud.framework.constants.CommonCoreConstants;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.ParameterCustomizer;
import org.springframework.core.MethodParameter;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;

/**
 *
 *
 * @author Athric Yu
 * 2024/11/20
 **/
public class CollectionParameterCustomizer implements ParameterCustomizer {

    private final TemporalPatternConfigProperties temporalPatternConfigProperties;

    public CollectionParameterCustomizer(TemporalPatternConfigProperties temporalPatternConfigProperties) {
        this.temporalPatternConfigProperties = temporalPatternConfigProperties;
    }

    @Override
    public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) {
        Type type = methodParameter.getGenericParameterType();
        if(parameterModel != null){
            if(type instanceof ParameterizedType){
                ParameterizedType parameterizedType = (ParameterizedType) type;
                if(Collection.class.isAssignableFrom((Class<?>) parameterizedType.getRawType())){
                    Type actualType = parameterizedType.getActualTypeArguments()[0];
                    Schema schema = new Schema();
                    schema.setType("array");
                    if(actualType instanceof Class){
                        Class<?> actualClass = (Class<?>) actualType;
                        Schema contentSchema = new Schema();
                        if(CommonCoreConstants.dateTimeClasses.contains(actualClass)){
                            contentSchema.setType("dateTime");
                        }else if(CommonCoreConstants.dateClasses.contains(actualClass)){
                            contentSchema.setType("date");
                        }else{
                            contentSchema.setType(actualClass.getSimpleName().toLowerCase());
                        }
                        schema.setContentSchema(contentSchema);
                    }
                    parameterModel.setSchema(schema);
                }
            }
        }
        return parameterModel;
    }

}
